import {Injectable, Type} from '@angular/core';
import {Blade} from './blade/blade.model';
import {BladeData} from './blade/blade-data';
import {ToolbarService} from './toolbar.service';
import {DialogService} from './dialog.service';
import * as $ from 'jquery'

// import { DialogResult } from './dialog/dialog.component';
/**
 * 弹出层服务对象
 * @Injectable标识一个类可以被注入器实例化
 * @export
 * @class BladeService
 */
@Injectable()
export class BladeService {
  /**
   * 弹出层数组
   *
   * @type {Blade[]}
   * @memberOf BladeService
   */
  public blades: Blade[] = [];
  /**
   * 当前激活的的弹出层
   *
   * @type {Blade}
   * @memberOf BladeService
   */
  public currentBlade: Blade;

  /**
   * Creates an instance of BladeService.
   * @param {DialogService} dialogService
   *
   * @memberof BladeService
   */

  constructor(private dialogService: DialogService) {

  }

  /**
   * 显示弹出层
   *
   * @param {Blade} blade
   * @param {Blade} parentBlade
   *
   * @memberOf BladeService
   */
  showBlade(blade: Blade, parentBlade?: Blade): Promise<boolean> {
    let xindex = 0;
    return new Promise((resolve, reject) => {
      if (parentBlade != null) {
        if (parentBlade.childrenBlades != null
          && parentBlade.childrenBlades.length > 0) {
          xindex = parentBlade.bladeData.xindex;
          let existingblade = parentBlade.childrenBlades[0];
          existingblade.bladeContainerInstance
            .beforeClosing()
            .then(res => {
              if (res) {
                this.closeBlade(existingblade);
                blade.parentBlade = parentBlade;
                blade.parentBlade.childrenBlades.push(blade);
                blade.bladeData.xindex = xindex + 1;
                this.blades.splice(blade.bladeData.xindex + 1, 0, blade);
                resolve(true);
              }
            })
            .catch();
        }
        else {

          xindex = parentBlade.bladeData.xindex;
          blade.parentBlade = parentBlade;
          blade.parentBlade.childrenBlades.push(blade);
          blade.bladeData.xindex = xindex + 1;
          this.blades.splice(blade.bladeData.xindex + 1, 0, blade);
          resolve(true);
        }

      }
      else {
        xindex = this.blades.length;
        blade.bladeData.xindex = xindex;
        this.blades.splice(xindex, 0, blade);
        resolve(true);
      }

    });
    // return Observable.create(observer => {
    //   this.currentBlade = blade;
    //   blade.bladeData.isLoading = true;
    //   blade.parentBlade = parentBlade;
    //   let xindex = 0;
    //   if (blade.parentBlade != null) {
    //     xindex = blade.parentBlade.bladeData.xindex;
    //     if (blade.parentBlade.childrenBlades.length > 0) {
    //       let existingblade = blade.parentBlade.childrenBlades[0];
    //       this.closeBlade(existingblade);
    //       blade.parentBlade.childrenBlades.push(blade);
    //       blade.bladeData.xindex = xindex + 1;
    //       this.blades.splice(blade.bladeData.xindex + 1, 0, blade);

    //     }
    //     else {
    //       blade.parentBlade.childrenBlades.push(blade);
    //       blade.bladeData.xindex = xindex + 1;
    //       this.blades.splice(blade.bladeData.xindex + 1, 0, blade);

    //     }
    //   }
    //   else {
    //     xindex = this.blades.length;
    //     blade.bladeData.xindex = xindex;
    //     this.blades.splice(xindex, 0, blade);

    //   }
    // }).subscribe();
  }

  /**
   * 关闭弹出层前检查是否更改
   *
   * @param {Blade} blade
   * @memberof BladeService
   */
  checkChangeCloseBlade(blade: Blade) {
    let index = this.blades.indexOf(blade);
    if (index != -1) {
      blade.bladeContainerInstance
        .beforeClosing()
        .then(res => {
          if (res) {
            this.closeBlade(blade)
          }
        })
    }

  }

  /**
   * 关闭弹出层
   *
   * @param {Blade} blade
   * @param {(data:any) => void} onClosed
   * @param {(data:any) => void} onBeforeClosing
   *
   * @memberOf BladeService
   */
  closeBlade(blade: Blade) {
    let index = this.blades.indexOf(blade);
    if (index != -1) {
      let childIndex = blade.parentBlade.childrenBlades.indexOf(blade);
      blade.parentBlade.childrenBlades.splice(childIndex, 1);
      this.blades.splice(index, 1);
      this.closeChildrenBlade(blade);

    }
  }

  /**
   * 关闭子弹出层
   *
   * @param {Blade} blade
   * @param {() => void} callback
   *
   * @memberOf BladeService
   */
  closeChildrenBlade(blade: Blade) {
    if (blade.childrenBlades.length) {
      for (let i = 0; i < blade.childrenBlades.length; i++) {
        this.closeBlade(blade.childrenBlades[i]);
      }
    }
  }

  /**
   * 创建一个弹出层
   *
   * @param {Type<any>} bladeContainerType 弹出层类型引用
   * @param {BladeData} bladeData 弹出层数据对象
   * @param {boolean} [isNew=false] 标示弹出层是否为新建对象或编辑对象
   * @param {*} [currentEntity=null]  传递到弹出层的对象
   * @returns {Blade}
   *
   * @memberof BladeService
   */
  createBlade(
    bladeContainerType: Type<any>,
    bladeData: BladeData,
    isNew: boolean = false,
    currentEntity: any = null,
    parentEntity: any = null
  ): Blade {
    let newBlade = new Blade(bladeContainerType);
    newBlade.bladeData = bladeData;
    newBlade.bladeService = this;
    newBlade.toolbarService = new ToolbarService();
    newBlade.dialogService = this.dialogService;
    newBlade.isNew = isNew;
    newBlade.currentEntity = currentEntity;
    newBlade.parentEntity = parentEntity;
    setTimeout(() => {
      if ($('#content')[0]) {
        $('#content').scrollLeft($('#content')[0].scrollWidth);
      }
    }, 500);
    return newBlade;
  }

  /**
   * 根据ID查询弹出层
   *
   * @param {string} id
   * @returns {Blade}
   *
   * @memberof BladeService
   */
  findBlade(id: string): Blade {
    return this.blades.find(o => o.bladeData.id == id)
  }

  /**
   * 设置错误信息
   *
   * @param {any} msg
   * @param {any} blade
   *
   * @memberOf BladeService
   */
  private setError(msg: string, blade: Blade) {
    blade.bladeData.isLoading = false;
    blade.bladeData.error = msg;
  }
}


// WEBPACK FOOTER //
// ./src/app/shared/blade.service.ts
